package com.liliudong.keelflower.common.gen;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class CodeGenerator {

    private final DataSourceConfig dataSourceConfig;

    private final static String OUT_PUT_DIR = "D://";
    private final static String AUTHOR = "liudong.li";

    public void gen() {
        AutoGenerator autoGenerator = new AutoGenerator();

        StrategyConfig strategyConfig = new StrategyConfig();
//        strategyConfig.setInclude(tables);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setEntityTableFieldAnnotationEnable(true);

        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.liliudong.keelflower");
        packageConfig.setModuleName("upms.biz");
        packageConfig.setMapper("dao");

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(OUT_PUT_DIR);
        globalConfig.setFileOverride(true);
        globalConfig.setOpen(true);
        globalConfig.setAuthor(AUTHOR);
        globalConfig.setBaseResultMap(true);
        globalConfig.setDateType(DateType.TIME_PACK);
        globalConfig.setEntityName("%s");
        globalConfig.setMapperName("%sDao");
        globalConfig.setXmlName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        globalConfig.setIdType(IdType.ASSIGN_ID);


        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.setTemplate(new TemplateConfig());
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setTemplateEngine(new VelocityTemplateEngine());

        autoGenerator.execute();
    }

}
